<?php
/**
* Letterman Newsletter Component
* 
* @package Letterman
* @author Soeren
* @copyright Soeren Eberhardt <soeren@virtuemart.net>
*   (who just needed an easy and *working* Newsletter component for Mambo 4.5.1 and mixed up Newsletter and YaNC)
* @copyright Mark Lindeman <mark@pictura-dp.nl> 
*    (parts of the Newsletter component by Mark Lindeman; Pictura Database Publishing bv, Heiloo the Netherland)
* @copyright Adam van Dongen <adam@tim-online.nl>
*    (parts of the YaNC component by Adam van Dongen, www.tim-online.nl)
*    
* @copyright Philip Sylvain <contact@sphilip.com>
*  	(MVC refactorization and AJAX)
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport('joomla.application.component.controller');

class LettermanControllerSubscribers extends JController
{
	/**
	 * Custom Constructor
	 */
	function __construct( $default = array() )
	{
		// Set a default view if none exists
		if ( ! JRequest::getCmd( 'view' ) ) {
			
			JRequest::setVar('view', 'subscribers' );
		}
		
		parent::__construct( $default );

		$this->registerTask( 'apply', 'save' );
		$this->registerTask( 'add', 'edit' );
	}
	
	function edit()
	{
		//create the view
		$view = & $this->getView('subscribers', 'html');
		
		// Get/Create the model
		$model = & $this->getModel('Subscribers');
		
		// Push the model into the view (as default)
		$view->setModel($model, true);
		
		$view->display('form');
		
	}
	
	
	function save()
	{
		// Check for request forgeries
		JRequest::checkToken() or die( 'Invalid Token' );
		
		// Get/Create the model
		$model = & $this->getModel('Subscribers');
		
		$url = 'index.php?option=com_letterman&controller=subscribers' ;
		
		if ( false ===  $model->save() ) {

		    $this->edit();
		    
		} else {

		  	if ( 'apply' == $this->getTask() ) {
		  		
		  		$row =& $model->getRow();
		  		$url = $url . '&task=edit&id=' . $row->id ;
		  	}
		  	
		  	$msg = JText::_( 'Successfully saved subscriber' ) ;
		    $this->setRedirect( $url , $msg );
		}
	}
	
	
	function cancel()
	{
		$this->setRedirect( 'index.php?option=com_letterman&controller=subscribers' );
	}
	
	
	function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or die( 'Invalid Token' );
		
		$model = & $this->getModel('Subscribers');
		
		if ( $model->remove() ) {
			
			$msg = JText::sprintf('Subscriber(s) successfully deleted', count($model->getCid()));
			$this->setRedirect( 'index.php?option=com_letterman&controller=subscribers', $msg );
			
		} else {
			
			$this->setRedirect( 'index.php?option=com_letterman&controller=subscribers' );
		}	
	}
	
	function validate()
	{
		$this->_validate(true);
	}
	
	function invalidate()
	{
		$this->_validate(false);
	}
	
	function _validate($bool)
	{
		$model = & $this->getModel('Subscribers');
		
		if ( $model->validate($bool) ) {
			
			$state = $bool ? 'validated' : 'invalidated' ;
			$msg = JText::sprintf('Subscriber(s) successfully '. $state , count($model->getCid()));
			$this->setRedirect( 'index.php?option=com_letterman&controller=subscribers', $msg );
			
		} else {
			
			$this->setRedirect( 'index.php?option=com_letterman&controller=subscribers' );
		}
	}
	
}

